package com.upeoe.lucrocrawler.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author ven_hxz
 * @create 2016-05-22 下午9:53
 */
public class FileKit {
    /**
     * 指定路径创建一个文件
     *
     * @param content  写入的内容
     * @param filePath 指定的路径
     * @param isAppend 是否为追加文件模式
     * @return
     */
    public static boolean writerFile(String content, String filePath, boolean isAppend) {
        boolean isSuccess;

        int index = filePath.lastIndexOf("/");
        String folder = filePath.substring(0, index);
        File dirs = new File(folder);
        isSuccess = dirs.mkdirs();
        File file = new File(filePath);

        try {
            isSuccess = file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        FileWriter writer = null;
        try {
            writer = new FileWriter(file, isAppend);
            writer.write(content);
            writer.flush();
        } catch (IOException e) {
            isSuccess = false;
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return isSuccess;
    }
}
